#include "activation_udp.h"
#include <QDebug>

activation_udp::activation_udp(QObject *parent)
{
    printf("Qwb01010\n");
    this->udpSocket = new QUdpSocket;
    this->timer = new QTimer(this);
    this->isStarted = false;
    this->port = 7070;
    this->broadcast_num = 0;
    this->udp_send_info = "";
    printf("Qwb020202\n");

    connect(timer, SIGNAL(timeout()), this, SLOT(mTimeOut()));
    timer->start(4000);
    printf("Qwb030303\n");
}

void activation_udp::mTimeOut()
{
    FILE *fp;
    fp = fopen("/home/kylin/wb.txt","a+");
    fprintf(fp,"Qwb888\n");
    int length=0;
    this->broadcast_num += 1;
    if(this->broadcast_num > 5)
    {
        this->timer->stop();
        fclose(fp);
        return ;
    }

    length = this->udpSocket->writeDatagram(this->udp_send_info.toLatin1(),this->udp_send_info.length(),QHostAddress::Broadcast,this->port);
    fprintf(fp,"%s %d\n",this->udp_send_info,length);
    fclose(fp);
}

void activation_udp::init_udpinfo(QString init_serial_num,QString init_hardware_coding)
{
    printf("Qwb444\n");
    this->udp_send_info = init_serial_num + init_hardware_coding;
    printf("Qwb555\n");
}
